/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;

using Gtk;
using Glade;
using Mono.Unix;

namespace DCSharp.Gui
{
	public class VisibleColumnsWindow : Dialog
	{
		private TreeViewColumn[] inactiveColumns;
		private TreeView treeView;
		private ListStore store;

		#region Constructors

		public VisibleColumnsWindow(TreeViewColumn[] columns, Window parent,
			params TreeViewColumn[] inactiveColumns) :
			base(Catalog.GetString("Visible Columns"), parent, DialogFlags.NoSeparator)
		{
			this.inactiveColumns = inactiveColumns;

			BorderWidth = 6;
			WindowPosition = WindowPosition.CenterOnParent;
			AddButton(Stock.Close, ResponseType.Close);

			// Create the widgets
			VBox vbox = new VBox(false, 6);
			vbox.BorderWidth = 6;
			VBox.PackStart(vbox, true, true, 0);

			Label label = new Label(Catalog.GetString("Select the columns you want to show."));
			label.Xalign = 0;
			vbox.PackStart(label, false, true, 0);

			ScrolledWindow sw = new ScrolledWindow();
			sw.HscrollbarPolicy = PolicyType.Automatic;
			sw.VscrollbarPolicy = PolicyType.Automatic;
			sw.ShadowType = ShadowType.In;
			vbox.PackStart(sw, true, true, 0);

			treeView = new TreeView();
			treeView.HeadersVisible = false;
			sw.Add(treeView);

			VBox.ShowAll();

			// Columns
			TreeViewColumn column;

			CellRendererToggle toggleRenderer = new CellRendererToggle();
			toggleRenderer.Toggled += OnColumnToggled;
			column = treeView.AppendColumn(null, toggleRenderer);
			column.SetCellDataFunc(toggleRenderer, SetToggleColumn);

			CellRendererText textRenderer = new CellRendererText();
			column = treeView.AppendColumn(Catalog.GetString("Name"),
				textRenderer);
			column.SetCellDataFunc(textRenderer, SetNameColumn);

			// Model
			store = new ListStore(typeof(TreeViewColumn));
			treeView.Model = store;

			foreach (TreeViewColumn c in columns)
			{
				store.AppendValues(c);
			}
		}

		#endregion

		#region Methods

		public static void ConnectToWidget(Widget widget, Action action,
			TreeViewColumn[] columns, params TreeViewColumn[] inactiveColumns)
		{
			if (widget == null)
			{
				throw new ArgumentNullException("widget");
			}
			if (action == null)
			{
				throw new ArgumentNullException("action");
			}

			VisibleColumnsWindow columnsWindow = null;

			// Add the action handler
			action.Activated += delegate
			{
				if (columnsWindow == null)
				{
					columnsWindow = new VisibleColumnsWindow(columns,
						widget.Toplevel as Window, inactiveColumns);

					columnsWindow.Response += delegate
					{
						columnsWindow.Destroy();
					};
					columnsWindow.Destroyed += delegate
					{
						columnsWindow = null;
					};
				}
				columnsWindow.Present();
			};

			// Handle the destroyed event
			widget.Destroyed += delegate
			{
				if (columnsWindow != null)
				{
					columnsWindow.Destroy();
				}
			};
		}

		private void SetToggleColumn(TreeViewColumn column,
			CellRenderer renderer, TreeModel model, TreeIter iter)
		{
			TreeViewColumn c = (TreeViewColumn)model.GetValue(iter, 0);
			CellRendererToggle toggleRenderer = (CellRendererToggle)renderer;

			toggleRenderer.Active = c.Visible;
			toggleRenderer.Activatable = Array.IndexOf(inactiveColumns, c) == -1;
		}

		private void SetNameColumn(TreeViewColumn column,
			CellRenderer renderer, TreeModel model, TreeIter iter)
		{
			TreeViewColumn c = (TreeViewColumn)model.GetValue(iter, 0);
			CellRendererText textRenderer = (CellRendererText)renderer;

			textRenderer.Text = c.Title;
		}

		private void OnColumnToggled(object obj, ToggledArgs args)
		{
			TreeIter iter;
			if (store.GetIterFromString(out iter, args.Path))
			{
				TreeViewColumn c = (TreeViewColumn)store.GetValue(iter, 0);
				c.Visible = !c.Visible;
			}
		}

		#endregion
	}
}
